package edu.avans.hartigehap.service.jpa;

import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;

import edu.avans.hartigehap.domain.MenuPizza;
import edu.avans.hartigehap.domain.MenuTopping;
import edu.avans.hartigehap.domain.StateException;
import edu.avans.hartigehap.domain.WebDish;
import edu.avans.hartigehap.domain.WebMenuItem;
import edu.avans.hartigehap.repository.WebDishRepository;
import edu.avans.hartigehap.repository.WebMenuItemRepository;
import edu.avans.hartigehap.repository.WebMenuItemRepositoryImpl;
import edu.avans.hartigehap.service.WebDishService;

@Service("webPizzaOrderService")
@Repository
@Transactional(rollbackFor = StateException.class)
public class WebDishServiceImpl implements WebDishService {
	final Logger logger = LoggerFactory.getLogger(WebDishServiceImpl.class);

	@Autowired
	private WebDishRepository webDishRepository;
	@Autowired
	private WebMenuItemRepositoryImpl webMenuItemRepositoryCustom;
	@Autowired
	private WebMenuItemRepository webMenuItemRepository;

	@Override
	public WebMenuItem saveMenuItem(WebMenuItem menuItem) {
		return webMenuItemRepository.save(menuItem);
	}

	@Transactional(readOnly=true)
	public List<WebDish> findAll() {
		return Lists.newArrayList(webDishRepository.findAll());
	}

	@Transactional(readOnly = true)
	public WebMenuItem findMenuItemById(Long id) {
		return webMenuItemRepository.findById(id);
	}

	@Transactional(readOnly = true)
	public WebDish findById(Long id) {
		return webDishRepository.findById(id);
	}


	public WebDish save(WebDish webDish) {
		return webDishRepository.save(webDish);
	}


	public void delete(Long id) {
		webDishRepository.delete(id);
	}

	public void deleteMenuItem(Long id){
		webMenuItemRepository.delete(id);		
	}

	@Transactional(readOnly=true)
	public Page<WebDish> findAllByPage(Pageable pageable) {
		return webDishRepository.findAll(pageable);
	}

	@Transactional(readOnly=true)
	public WebDish fetchWarmedUp(Long webDishId) {
		logger.info("(fetchWarmedUp) diningTable id: " + webDishId);

		WebDish webDish = webDishRepository.findById(webDishId);

		logger.info("diningTable = " + webDish.getId());

		return webDish;
	}

	public void createNewWebMenuItem() {
		WebMenuItem webDish1 = new MenuPizza(new BigDecimal(5.50), "Pizza Margarita");
		WebMenuItem webDish2 = new MenuPizza(new BigDecimal(6.95), "Pizza Tonno");
		WebMenuItem webDish3 = new MenuPizza(new BigDecimal(8.50), "Pizza Shoarma");
		WebMenuItem webDish4 = new MenuPizza(new BigDecimal(7.50), "Pizza Pepperoni");
		WebMenuItem webDish5 = new MenuPizza(new BigDecimal(5.95), "Pizza Hawaii");
		WebMenuItem webDish6 = new MenuPizza(new BigDecimal(7.95), "Pizza Veggie");

		webMenuItemRepository.save(webDish1);
		webMenuItemRepository.save(webDish2);
		webMenuItemRepository.save(webDish3);
		webMenuItemRepository.save(webDish4);
		webMenuItemRepository.save(webDish5);
		webMenuItemRepository.save(webDish6);

		WebMenuItem webTopping1 = new MenuTopping(new BigDecimal(0.50), "Kaas");
		WebMenuItem webTopping2 = new MenuTopping(new BigDecimal(0.50), "Bacon");

		webMenuItemRepository.save(webTopping1);
		webMenuItemRepository.save(webTopping2);
	}
	//Nieuwe pizza aanmaken
	public void createNewPizza(BigDecimal price, String name){
		WebMenuItem webDish = new MenuPizza(price, name);
		webMenuItemRepository.save(webDish);
	}

	@Transactional(readOnly=true)
	public List<WebMenuItem> findAllPizzas() {
		List<WebMenuItem> pizzas = webMenuItemRepositoryCustom.findAllPizzas();
		return pizzas;
	}

	@Transactional(readOnly=true)
	public List<WebMenuItem> findAllToppings() {
		List<WebMenuItem> toppings = webMenuItemRepositoryCustom.findAllToppings();
		return toppings;
	}

	@Override
	public WebDish findByName(String name) {
		Iterator<WebDish> dishes = webDishRepository.findAll().iterator();

		WebDish dish = null;
		while (dishes.hasNext()) {
			dish = dishes.next();
			if(dish.getDishName().equals(name)) {
				return dish;
			}
		}
		return null;
	}

}
